package com.yuke.cloud.service.uac.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.uac.entity.AccGroupRole;
import com.yuke.cloud.service.uac.service.AccGroupRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zn on 2019/11/11.
*/
@Api(value = "WEB - AccGroupRoleController", tags = "处理AccGroupRole相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/acc/group_role")
public class AccGroupRoleController extends BaseController {
    @Resource
    private AccGroupRoleService accGroupRoleService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "accGroupRole", value = "该数据记录的POJO") @RequestBody AccGroupRole accGroupRole) {
       if (accGroupRole != null){
           AccGroupRole bbb = new AccGroupRole();
           bbb.setGroupId(accGroupRole.getGroupId());
           List<AccGroupRole> aaa = accGroupRoleService.select(bbb);
           if (aaa!=null || aaa.size()>0){
               WrapMapper.error("该部门已绑定有角色！");
           }
           accGroupRoleService.save(accGroupRole);
           return WrapMapper.ok();
       }
        return WrapMapper.error("传入参数为空！");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        accGroupRoleService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "accGroupRole", value = "需要修改的内容") @RequestBody AccGroupRole accGroupRole) {
        accGroupRoleService.update(accGroupRole);
        return WrapMapper.ok();
    }

    @GetMapping("/{groupId}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<AccGroupRole> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long groupId) {
        AccGroupRole accGroupRole = accGroupRoleService.selectByKey(groupId);
        return WrapMapper.ok(accGroupRole);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccGroupRole>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<AccGroupRole> list = accGroupRoleService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }
    /**
     * 校验部门号唯一性.
     *
     * @param groupId the check user phone dto
     *
     * @return the wrapper
     */
    @GetMapping(value = "/checkGroupId")
    @ApiOperation(httpMethod = "GET", value = "校验部门号唯一性")
    public Wrapper<Boolean> checkGroupId(@RequestParam(defaultValue = "0") Long groupId) {
        logger.info(" 校验部门号唯一性 checkGroupId={}", groupId);
        Example example = new Example(AccGroupRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId", groupId);

        int result = accGroupRoleService.selectCountByExample(example);
        if (result < 1){
            return WrapMapper.ok();
        }
        return WrapMapper.error("该部门已绑定角色！"+result);
    }
}
